#include <stdio.h>
#include <string.h>

int line_num(FILE * fp)
{   char ch;
    int num = 0;
    fseek(fp,0,SEEK_SET);
    for (int i = 0; EOF != (ch = fgetc(fp)); i++)
    {
        if ('\n' == ch)
        {
            num++;
        }
    }
    return num;
}

void clear_and_write(const char *dest1,const char *dest2,const char *dest3)
{   
    char ch;
    //打开文件
    FILE *fp1 = fopen(dest1,"w+");
    FILE *fp2 = fopen(dest2,"r");

    for (int i = 0; EOF != (ch = fgetc(fp2)); i++)
    {
        fputc(ch,fp1);
    }

    //关闭文件
    fclose(fp1);
    fclose(fp2);


    //打开文件
    fp1 = fopen(dest1,"a+");
    FILE *fp3 = fopen(dest3,"r");

    for (int i = 0; EOF != (ch = fgetc(fp3)); i++)
    {
        fputc(ch,fp1);
    }

    //关闭文件
    fclose(fp1);
    fclose(fp3);
    return;

}


typedef struct user
{
    int id;
    int grade;
}user;

user u_1[10] = {{.id = 1 , .grade = 60},
                {.id = 2 , .grade = 70},
                {.id = 3 , .grade = 78},
                {.id = 3 , .grade = 78},
                {.id = 3 , .grade = 78},
                {.id = 3 , .grade = 78},
                {.id = 3 , .grade = 78},

};
int main(int argc, char const *argv[])
{
    FILE * fp = fopen("lisi.txt" , "a+");
    for (int i = 0; i < 7; i++)
    {
        fprintf(fp,"学号%d,成绩%d \n",u_1[i].id,u_1[i].grade);
    }
    line_num(fp);
    printf("空行的个数为%d \n",line_num(fp));
    fclose(fp);

    clear_and_write(argv[1],argv[2],argv[3]);
    return 0;
} 
